package com.rabbiter.oes.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rabbiter.oes.entity.ApiResult;
import com.rabbiter.oes.entity.Teacher;
import com.rabbiter.oes.serviceimpl.TeacherServiceImpl;
import com.rabbiter.oes.util.ApiResultHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class TeacherController {

    private TeacherServiceImpl teacherService;
    @Autowired
    public TeacherController(TeacherServiceImpl teacherService){
        this.teacherService = teacherService;
    }

    /**
     * 分页查询所有教师信息
     * @param page 当前页码，从1开始
     * @param size 每页显示记录数
     * @return ApiResult 包含状态码、提示消息和分页数据结果
     */
    @GetMapping("/teachers/{page}/{size}")
    public ApiResult findAll(@PathVariable Integer page, @PathVariable Integer size){
        Page<Teacher> teacherPage = new Page<>(page,size);
        IPage<Teacher> teacherIPage = teacherService.findAll(teacherPage);

        return ApiResultHandler.buildApiResult(200,"查询所有教师",teacherIPage);
    }

    /**
     * 根据教师ID获取教师详细信息
     * @param teacherId 教师唯一标识符
     * @return ApiResult 包含状态码、提示消息和单个教师数据
     */
    @GetMapping("/teacher/{teacherId}")
    public ApiResult findById(@PathVariable("teacherId") Integer teacherId){
        return ApiResultHandler.success(teacherService.findById(teacherId));
    }

    /**
     * 根据教师ID删除教师记录
     * @param teacherId 要删除的教师唯一标识符
     * @return ApiResult 包含操作结果状态和提示消息
     */
    @DeleteMapping("/teacher/{teacherId}")
    public ApiResult deleteById(@PathVariable("teacherId") Integer teacherId){
        return ApiResultHandler.success(teacherService.deleteById(teacherId));
    }

    /**
     * 更新教师信息
     * @param teacher 包含更新后教师信息的实体对象（通过请求体传递）
     * @return ApiResult 包含更新操作结果和更新后的数据
     */
    @PutMapping("/teacher")
    public ApiResult update(@RequestBody Teacher teacher){
        return ApiResultHandler.success(teacherService.update(teacher));
    }

    /**
     * 新增教师信息
     * @param teacher 包含新教师信息的实体对象（通过请求体传递）
     * @return ApiResult 包含新增操作结果和生成的主键信息
     */
    @PostMapping("/teacher")
    public ApiResult add(@RequestBody Teacher teacher){
        return ApiResultHandler.success(teacherService.add(teacher));
    }
}
